/**
 * Copyright (c) 2013 onionframework.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.onionframework.core.components;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;



/**
 * Onion组件
 * @author allan
 *
 */
@XStreamAlias("component")
public class ComponentDescriptor{

	@XStreamAsAttribute protected String groupId;
	@XStreamAsAttribute protected String artifactId;
	protected String description;
	@XStreamOmitField protected String name;
	@XStreamOmitField protected String version;
	@XStreamImplicit(itemFieldName = "config") protected List<ConfigDefinition> configDefinitions = new ArrayList<ConfigDefinition>();
	@XStreamImplicit(itemFieldName = "optional") protected List<OptionalPart> optionalParts = new ArrayList<OptionalPart>();
	@XStreamAsAttribute protected int order = 100;
	
	public ComponentDescriptor(){
	
	}
	
	public void init(){
		if(configDefinitions == null)
			configDefinitions = new ArrayList<ConfigDefinition>();
		if(optionalParts == null)
			optionalParts = new ArrayList<OptionalPart>();
		
		
		Element pomElement = null;
		try {
			InputStream in = this.getClass()
				.getResourceAsStream("/META-INF/maven/" + groupId + "/" 
					+ artifactId + "/pom.xml");
			
			if(in != null)
				pomElement = DocumentHelper.parseText(
					IOUtils.toString(in,"UTF-8")).getRootElement();
			
		} catch (Exception e) {
			throw new RuntimeException("组件描述符加载失败：" + groupId + "." + this.artifactId,e);
		}

		if(pomElement != null){
			this.version = pomElement.elementTextTrim("version");
			this.name = pomElement.elementTextTrim("name");
			if(StringUtils.isBlank(this.description))
				this.description = pomElement.elementText("description");
		}else{
			this.name = this.artifactId;
			this.version = "未知版本";
			if(StringUtils.isBlank(this.description))
				this.description = "无";
		}
	}
	
	public List<ConfigDefinition> getConfigDefinitions(){
		return this.configDefinitions;
	}
	
	/**
	 * @return the id
	 */
	public String getArtifactId() {
		return this.artifactId;
	}

	/**
	 * @return the groupId
	 */
	public String getGroupId() {
		return groupId;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @return the order
	 */
	public int getOrder() {
		return order;
	}

	/**
	 * @return the optionalParts
	 */
	public List<OptionalPart> getOptionalParts() {
		return optionalParts;
	}
	
	public String toString(){
		return new ToStringBuilder(this,ToStringStyle.SHORT_PREFIX_STYLE).append("groupId", this.groupId)
			.append("artifactId",this.artifactId).append("version",this.version)
			.toString();
	}
}
